package threadPool;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

public class CompletableFutureDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        CompletableFuture<Void> completableFuture = CompletableFuture.runAsync(() -> {
            System.out.println(Thread.currentThread().getName() + "\t no return, some update");
        });
        System.out.println(completableFuture.get());

        CompletableFuture<Integer> completableFuture1 = CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName() + "\t has return");
//            int temp = 10 / 0;
            return 200;
        });

        System.out.println(completableFuture1.whenComplete((t, u) -> {
            System.out.println("***********t " + t);    // 正常时的返回结果
            System.out.println("***********u " + u);    // 错误信息
        }).exceptionally(f -> {
            System.out.println("**exception: " + f.getMessage());
            return -100;    // 运行错误时，返回的值
        }).get());

    }
}
